﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using VisionApp.Interface;
using VisionApp.Entity;
using System.Runtime.InteropServices;
using System.IO;
using OpenCvSharp;
using System.Configuration;
using System.Threading;

namespace VisionApp.Camera
{
    public class FileCamera : ObservableObject, ICamera
    {
        public HImageContainer ImCtnr { get; set; }
        public bool Connected { get; set; }
        public List<string> DeviceIDList { get ; set; }
        public CameraInfo CurrentCameraInfo { get; set; }
        private string _path = System.Environment.CurrentDirectory + "/Images/Sample/";
        private string[] _imgFileNameList;
        private int _imgID = 1;
        private Logger _log;
        private int bAutoExposure = Convert.ToInt32(ConfigurationManager.AppSettings["AutoExposure"]);
        private int Exposure = Convert.ToInt32(ConfigurationManager.AppSettings["Exposure"]);
        private int bAutoGain = Convert.ToInt32(ConfigurationManager.AppSettings["AutoGain"]);
        private int maxExposure = Convert.ToInt32(ConfigurationManager.AppSettings["MaxExposure"]);
        private int maxGain = Convert.ToInt32(ConfigurationManager.AppSettings["MaxGain"]);
        private Thread hShowImageThreadHandle;
        public bool g_bExit = false;
        public FileCamera(ILogger log)
        {
            _log = (Logger)log;
            ImCtnr = new HImageContainer();
            _imgFileNameList = Directory.GetFiles(_path, "*.bmp");
        }

        public int Init()
        {
            try
            {
                Connected = true;
                hShowImageThreadHandle = new Thread(showImage);
                hShowImageThreadHandle.Start();
                return 0;
            }
            catch (Exception e)
            {
                _log.Error("Init Error " + e.ToString());
                return -1;
            }
        }

        public int GrabImg()
        {
            try
            {
                Mat image = Cv2.ImRead(_imgFileNameList[0], ImreadModes.Unchanged);
                byte[] data = new byte[image.Cols * image.Rows * 3];
                Marshal.Copy(image.Data, data, 0, image.Cols * image.Rows * 3);
                ImCtnr = new HImageContainer();
                ImCtnr.TextWidth = image.Cols;
                ImCtnr.TextHeight = image.Rows;
                ImCtnr.ImGray = data;
                return 0;
            }
            catch(Exception e)
            {
                return -1;
            }
        }
        public int Exit()
        {
            g_bExit = true;
            hShowImageThreadHandle?.Abort();
            Connected = false;
            return 0;
        }

        public int StopRecord()
        {
            return 0;
        }
        private void showImage()
        {
            try
            {
                System.Threading.Thread.Sleep(100);
                while (true)
                {
                    System.Threading.Thread.Sleep(100);
                    if (ImCtnr == null)
                    {
                        continue;
                    }
                    if (ImCtnr.ImGray == null)
                    {
                        continue;
                    }
                    HImageContainer hic = new HImageContainer();
                    lock (ImCtnr)
                    {
                        hic.TextWidth = ImCtnr.TextWidth;
                        hic.TextHeight = ImCtnr.TextHeight;
                        hic.ImGray = (byte[])ImCtnr.ImGray.Clone();
                        hic.ID = ImCtnr.ID;
                    }
                    Messenger.Default.Send<HImageContainer>(hic);
                }
                _log.Imformation("显示线程结束");
            }
            catch (Exception e)
            {
                _log.Error("showImage:" + e.ToString());
            }
        }
    }
}
